package server;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.nio.file.Files;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @file Download_Handler.java
 *
 * @version 0.1
 * @author James Dibble 10009689
 */
public class Download_Handler implements Runnable {
    // <editor-fold defaultstate="collapsed" desc="Attributes">

    Resource thisResource;
    Socket sSocket;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructors">
    public Download_Handler(Resource thisResource, Socket thisSocket) {
        this.thisResource = thisResource;
        this.sSocket = thisSocket;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Methods">
    @Override
    public void run() {
        try {
            File thisFile = thisResource.getResourceAsFile();
            byte[] byteBuff = new byte[(int) thisFile.length()];
            byteBuff = Files.readAllBytes(thisFile.toPath());
            sSocket.getOutputStream().flush();
            sSocket.getOutputStream().write(byteBuff);
            sSocket.getOutputStream().flush();
            System.out.println(String.format("Server->%s(%s): %s",
                    sSocket.getInetAddress().getHostName(),
                    sSocket.getInetAddress().toString(),
                    "File uploaded."));
        } // </editor-fold>
        catch (IOException ex) {
            //TODO: Add exception handling.
        }
    }
    // </editor-fold>
}
